package com.vr.cloudpicture.controller;

import cn.hutool.core.util.StrUtil;
import com.vr.cloudpicture.annotation.AuthCheck;
import com.vr.cloudpicture.api.aliyunoutpicture.AliYunAiApi;
import com.vr.cloudpicture.api.aliyunoutpicture.model.CreateOutPaintingTaskResponse;
import com.vr.cloudpicture.api.aliyunoutpicture.model.QueryOutPaintingTaskResponse;
import com.vr.cloudpicture.api.imagesearch.ImageSearchApiFacade;
import com.vr.cloudpicture.api.imagesearch.model.ImageSearchResult;
import com.vr.cloudpicture.common.BaseResponse;
import com.vr.cloudpicture.common.PageResult;
import com.vr.cloudpicture.common.ResultUtils;
import com.vr.cloudpicture.constant.UserConstant;
import com.vr.cloudpicture.exception.ErrorCode;
import com.vr.cloudpicture.exception.ThrowUtils;
import com.vr.cloudpicture.manager.auth.annotation.SaSpaceCheckPermission;
import com.vr.cloudpicture.manager.auth.model.SpaceUserPermissionConstant;
import com.vr.cloudpicture.model.dto.DeleteRequest;
import com.vr.cloudpicture.model.dto.picture.*;
import com.vr.cloudpicture.model.dto.user.UserLogin;
import com.vr.cloudpicture.model.entity.Picture;
import com.vr.cloudpicture.model.vo.PictureTagCategoryVO;
import com.vr.cloudpicture.model.vo.PictureVO;
import com.vr.cloudpicture.service.IPictureService;
import com.vr.cloudpicture.service.IUserService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @author hzh
 * @date 2025/05/07
 */
@Slf4j
@RestController()
@RequestMapping("/picture")
@RequiredArgsConstructor
public class PictureController {

    private final IPictureService pictureService;
    private final IUserService userService;
    private final AliYunAiApi aliYunAiApi;

    @ApiOperation(value = "上传文件方式上传图片")
    @PostMapping("/upload")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_UPLOAD)
    public BaseResponse<PictureVO> uploadPicture(@RequestPart("file") MultipartFile multipartFile,
                                                 PictureUploadRequest pictureUploadRequest,
                                                 HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        return ResultUtils.success(pictureService.uploadPicture(multipartFile, pictureUploadRequest, loginUser));
    }

    @ApiOperation(value = "通过url方式上传图片")
    @PostMapping("/upload/url")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_UPLOAD)
    public BaseResponse<PictureVO> uploadPictureByUrl(@RequestBody PictureUploadRequest pictureUploadRequest, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        String fileUrl = pictureUploadRequest.getFileUrl();
        return ResultUtils.success(pictureService.uploadPicture(fileUrl, pictureUploadRequest, loginUser));
    }

    @ApiOperation(value = "批量抓取图片")
    @PostMapping("/upload/batch")
    @AuthCheck(mustRole = UserConstant.USER_ROLE_ADMIN)
    public BaseResponse<Integer> uploadPictureByBatch(@RequestBody PictureUploadByBatchRequest pictureUploadByBatchRequest, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        return ResultUtils.success(pictureService.uploadPictureByBatch(pictureUploadByBatchRequest, loginUser));
    }

    @ApiOperation(value = "通过id获取图片,但仅仅只有管理员可用")
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.USER_ROLE_ADMIN)
    public BaseResponse<Picture> getPictureById(Long id) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR);
        Picture picture = pictureService.getById(id);
        ThrowUtils.throwIf(picture == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(picture);
    }

    @ApiOperation(value = "通过id获取图片,谁都可以用")
    @GetMapping("/get/vo")
    public BaseResponse<PictureVO> getPictureVOById(Long id, HttpServletRequest request) {
        return ResultUtils.success(pictureService.getPictureVOById(id, request));
    }

    @ApiOperation(value = "通过id删除图片,给管理员用的")
    @PostMapping("/delete")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_DELETE)
    public BaseResponse<Boolean> deletePicture(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        return ResultUtils.success(pictureService.deletePicture(deleteRequest, request));
    }

    @ApiOperation(value = "更新图片,给管理员用的")
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.USER_ROLE_ADMIN)
    public BaseResponse<Boolean> updatePicture(@RequestBody PictureUpdateRequest pictureUpdateRequest, HttpServletRequest request) {
        return ResultUtils.success(pictureService.updatePicture(pictureUpdateRequest, request));
    }

    @ApiOperation(value = "编辑图片，给用户用的")
    @PostMapping("/edit")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public BaseResponse<Boolean> editPicture(@RequestBody PictureEditRequest pictureEditRequest, HttpServletRequest request) {
        return ResultUtils.success(pictureService.editPicture(pictureEditRequest, request));
    }

    @ApiOperation(value = "批量编辑图片")
    @PostMapping("/edit/batch")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public BaseResponse<Boolean> editPictureByBatch(@RequestBody PictureEditByBatchRequest pictureEditByBatchRequest, HttpServletRequest request) {
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        return ResultUtils.success(pictureService.editPictureByBatch(pictureEditByBatchRequest, loginUser));
    }

    @ApiOperation(value = "获取图片列表,仅管理员可见")
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.USER_ROLE_ADMIN)
    public BaseResponse<PageResult<Picture>> listPictureByPage(@RequestBody PictureQueryRequest pictureQueryRequest) {
        return ResultUtils.success(pictureService.listPictureByPage(pictureQueryRequest));
    }

    @ApiOperation(value = "获取图片列表,所有可见")
    @PostMapping("/list/page/vo")
    public BaseResponse<PageResult<PictureVO>> listPictureVOByPage(@RequestBody PictureQueryRequest pictureQueryRequest, HttpServletRequest request) {
        return ResultUtils.success(pictureService.listPictureVOByPage(pictureQueryRequest, request));
    }

    @ApiOperation(value = "获取图片标签和分类,变化比较")
    @GetMapping("/tag_category")
    public BaseResponse<PictureTagCategoryVO> listPictureTagCategory() {
        PictureTagCategoryVO pictureTagCategory = new PictureTagCategoryVO();
        List<String> tagList = Arrays.asList("热门", "搞笑", "生活", "高清", "艺术", "校园", "背景", "简历", "创意");
        List<String> categoryList = Arrays.asList("模板", "电商", "表情包", "素材", "海报");
        pictureTagCategory.setTagList(tagList);
        pictureTagCategory.setCategoryList(categoryList);
        return ResultUtils.success(pictureTagCategory);
    }

    @ApiOperation(value = "图片审核")
    @PostMapping("/review")
    @AuthCheck(mustRole = UserConstant.USER_ROLE_ADMIN)
    public BaseResponse<Boolean> reviewPicture(@RequestBody PictureReviewRequest pictureReviewRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(pictureReviewRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(pictureService.reviewPicture(pictureReviewRequest, request));
    }

    @ApiOperation(value = "以图搜图")
    @PostMapping("/search/picture")
    public BaseResponse<List<ImageSearchResult>> searchPictureByPicture(@RequestBody SearchPictureByPictureRequest searchPictureByPictureRequest) {
        boolean condition= searchPictureByPictureRequest==null || searchPictureByPictureRequest.getPictureId()==null || searchPictureByPictureRequest.getPictureId()<=0;
        ThrowUtils.throwIf(condition, ErrorCode.PARAMS_ERROR);
        Picture oldPicture = pictureService.getById(searchPictureByPictureRequest.getPictureId());
        ThrowUtils.throwIf(oldPicture==null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(ImageSearchApiFacade.searchImage(oldPicture.getPreviewUrl()));
    }

    @ApiOperation(value = "以色搜图")
    @PostMapping("/search/color")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_VIEW)
    public BaseResponse<List<PictureVO>> searchPictureByColor(@RequestBody SearchPictureByColorRequest searchPictureByColorRequest,HttpServletRequest request) {
        boolean condition=searchPictureByColorRequest==null || searchPictureByColorRequest.getSpaceId()<=0 || StrUtil.isBlank(searchPictureByColorRequest.getPicColor());
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser==null, ErrorCode.NOT_LOGIN_ERROR);
        ThrowUtils.throwIf(condition, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(pictureService.searchPictureByColor(searchPictureByColorRequest,loginUser));
    }
    @ApiOperation(value = "创建AI扩图任务")
    @PostMapping("/out-painting/create-task")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.PICTURE_EDIT)
    public BaseResponse<CreateOutPaintingTaskResponse> createPictureOutPaintingTask(@RequestBody CreatePictureOutPaintingRequest createPictureOutPaintingRequest,HttpServletRequest request) {
        boolean condition=createPictureOutPaintingRequest==null || createPictureOutPaintingRequest.getPictureId()==null || createPictureOutPaintingRequest.getPictureId()<=0;
        ThrowUtils.throwIf(condition, ErrorCode.PARAMS_ERROR);
        UserLogin loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser==null, ErrorCode.NOT_LOGIN_ERROR);
        return ResultUtils.success(pictureService.createPictureOutPaintingTask(createPictureOutPaintingRequest,loginUser));
    }

    @ApiOperation(value = "查询AI扩图任务")
    @GetMapping("/out-painting/get-task")
    public BaseResponse<QueryOutPaintingTaskResponse> getPictureOutPaintingTask(String taskId) {
        ThrowUtils.throwIf(StrUtil.isBlank(taskId), ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(aliYunAiApi.queryOutPaintingTask(taskId));
    }
}
